﻿@using Think9.Models;
@{ ViewBag.Title = "Index";
    Layout = "~/Areas/Shared/_LayuiList.cshtml"; }

<blockquote class="layui-elem-quote layui-text">
    【使用步骤】  1.定义统计指标； 2.将统计指标放置在统计表单元格中； 3.为统计指标条件中包含的条件参数赋值，赋值方式包括以下：a.在统计指标出现的位置(统计表单元格)为条件参数赋值; b.定义查询参数为条件参数赋值; c.定义动态行为条件参数赋值
</blockquote>
<span style="position: absolute;top: 20px; right: 20px;" id="blockquote_close">
    <i class="fa fa-close" style="color: #FFAB00;" id="tipsWhereStr"></i>
</span>
<fieldset class="table-fieldset" id="searchfield" style="display:none">
    <legend style="color:darkgrey">快速查询</legend>
    <div style="margin: 5px 5px 5px 5px">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <input type="text" name="key" id="key" placeholder="关键字" class="layui-input">
                </div>
                <div class="layui-inline">
                    <button type="button" class="layui-btn layui-btn-normal" id="search"><i class="layui-icon layui-icon-search"></i></button>
                    <button type='reset' class='layui-btn layui-btn-primary'><i class='layui-icon layui-icon-refresh'></i></button>
                </div>
            </div>
        </form>
    </div>
</fieldset>
<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
<script>
    layui.use(["table", "form", "exLayer", "exUtils", "miniPage"], function () {
        let table = layui.table;
        let form = layui.form;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;
        let miniPage = layui.miniPage;

        var openWH = miniPage.getOpenWidthHeight();
        let $ = layui.$;

        form.render(); //

        $("#blockquote_close").click(function () {
            $(".layui-elem-quote").attr("style", "display:none");
            $("#blockquote_close").attr("style", "display:none");
        });

        let ThisTable = table.render({
            elem: "#tableId",
            url: "/SysStats/IndexStats/GetListBySearch?key=" + $("#key").val(),
            limits: [10, 50, 100],
            limit: 10,
            method: "post",
            page: true,
            defaultToolbar: layui.device().mobile ? [{ title: '搜索', layEvent: 'searchShow', icon: 'layui-bg-blue layui-icon-search' }, { title: '显示所有数据', layEvent: 'refresh', icon: 'layui-bg-gray layui-icon-refresh' }] : [{ title: '搜索', layEvent: 'searchShow', icon: 'layui-bg-blue layui-icon-search' }, { title: '显示所有数据', layEvent: 'refresh', icon: 'layui-bg-gray layui-icon-refresh' }, 'filter', 'exports'],
            toolbar: "#toolbarTpl",
            cols: [[
                { type: "checkbox", fixed: 'left' },
                { type: "numbers", title: "NO.", fixed: 'left' },
                { field: "IndexId", title: "编码", width: 150, sort: true },
                { field: "IndexName", title: "名称", width: 200, sort: true },
                { field: "ParmStr", title: "已关联参数", hide: layui.device().mobile ? true : false, sort: true },
                { title: "操作", width: 120, align: "center", fixed: layui.device().mobile ? null : 'right', templet: "#operationTpl" }
            ]],
            done: function (res, curr, count) {
                if (layui.device().mobile) {
                    $("#batchDel").html('<i class="fa fa-trash-o"></i>删除');
                    $("#sort").html('<i class="fa fa-paste"></i>分类');
                    $("#add").html('<i class="fa fa-plus"></i>新建');
                }
            },
            text: {
                none: '未定义统计指标'
            }
        });

        //查询
        $("#search").click(function () {
            search();
        });

        table.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "add":
                    add();
                    break;
                case "sort":
                    sort();
                    break;
                case "searchShow":
                    searchShow();
                    break;
                case "batchDel":
                    batchDel();
                    break;
                case "refresh":
                    refresh();
                    break;
            }
        });

        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "edit":
                    edit(data.IndexId);
                    break;
                case "del":
                    del(data.IndexId);
                    break;
            }
        });

        function add() {
            exLayer.openMiddle("新建指标", "/SysStats/IndexStats/Add?frm=list", '100%', '100%', layui.device().mobile);
        }

        function sort() {
            exLayer.openMiddle("统计指标分类", "/SysBasic/Sort/List?classid=CAT_indexstats", '500px', '450px', layui.device().mobile);
        }

        function searchShow() {
            var display = $('#searchfield').css('display');
            if (display == 'none') {
                $("#searchfield").show();
            }
            else {
                $("#searchfield").hide();
            }
        }

        function search() {
            $("#searchfield").hide();

            ThisTable.reload({
                where: { key: $("#key").val() },
                page: { curr: 1 }
            });
            return false;
        }

        function refresh() {
            ThisTable.reload({
                where: { key: '' }
            });
        }

        function edit(IndexId) {
            exLayer.openMiddle("编辑指标", "/SysStats/IndexStats/Edit/" + IndexId, '100%', '100%', layui.device().mobile);
        }

        function del(id) {
            exLayer.confirm("确定要删除吗？", function () {
                exUtils.ajax("/SysStats/IndexStats/Delete", "get", { id: id }, true).done(function (response) {
                    exUtils.tableSuccessMsg(response.message);
                    $(".layui-laypage-btn")[0].click();
                }).fail(function (error) {
                    console.log(error);
                });
            })
        }

        function batchDel() {
            var idsStr = "";
            var checkStatus = table.checkStatus("tableId");
            var rows = checkStatus.data.length;
            if (rows > 0) {
                for (var i = 0; i < checkStatus.data.length; i++) {
                    idsStr += checkStatus.data[i].IndexId + ",";
                }
                exLayer.confirm("确定要删除吗？", function (index) {
                    layer.close(index);

                    if (idsStr) {
                        exUtils.ajax("/SysStats/IndexStats/BatchDel", "get", { idsStr: idsStr }, true).done(function (response) {
                            exUtils.tableSuccessMsg(response.message);
                            $(".layui-laypage-btn")[0].click();
                        }).fail(function (error) {
                            console.log(error);
                        });
                    }
                });
            }
            else {
                layer.msg("未选择有效数据");
            }
        }
    });
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
    <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="batchDel" id="batchDel"><i class="fa fa-trash-o"></i>批量删除</button>
    <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" lay-event="sort" id="sort"><i class="fa fa-paste"></i>分类管理</button>
    <button type="button" class="layui-btn layui-btn-warm layui-btn-sm" lay-event="add" id="add"><i class="fa fa-plus"></i>新建指标</button>
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    <a href="javascript:;" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" id="edit">编辑</a>
    <a href="javascript:;" class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" id="del">删除</a>
</script>